package com.jy.admin.rest;


import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.exception.common.ApplicationException;
import com.jy.admin.annotations.AdminSignIgnore;
import com.jy.admin.db.dao.CeAgentAccountDao;
import com.jy.admin.db.dao.CeMerchantDao;
import com.jy.admin.db.dao.RewardConfigDao;
import com.jy.admin.db.entity.RewardConfigEntity;
import com.jy.admin.enums.CurrentUserBean;
import com.jy.admin.services.cashPocket.vo.RewardRequest;
import com.jy.bo.common.conf.api.ApiResponse;
import org.apache.zookeeper.KeeperException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/mc")
public class MerchantController {
    private final static Logger LOGGER = LoggerFactory.getLogger(MerchantController.class);

    @Autowired
    CeAgentAccountDao ceAgentAccountDao;

    @Autowired
    CeMerchantDao ceMerchantDao;

    @Autowired
    RewardConfigDao rewardConfigDao;


    @AdminSignIgnore
    @RequestMapping("/reward")
    public ApiResponse<List<RewardConfigEntity>> queryReward(@RequestBody RewardRequest request) {
        LOGGER.info("====,clientCode:{}",request.getClientCode());
        List<RewardConfigEntity> rewardConfigEntities = rewardConfigDao.selectRewardByClientCodeAndBucket(request.getClientCode(), request.getBucket());
        return new ApiResponse<>(rewardConfigEntities);
    }

    @AdminSignIgnore
    @RequestMapping("/reward/bucket")
    public ApiResponse<List<String>> queryRewardBucket() {
        List<String> bucketList = rewardConfigDao.selectRewardBucket();
        return new ApiResponse<>(bucketList);
    }

    @AdminSignIgnore
    @RequestMapping("/reward/update")
    public ApiResponse updateReward(@RequestBody RewardRequest request) {
        LOGGER.info("====,clientCode:{}",request.getClientCode());
        RewardConfigEntity rewardConfigEntities = rewardConfigDao.selectRewardOne(request.getClientCode(), request.getBucket(), request.getTargetFinishValue());
        if (rewardConfigEntities == null){
            throw new ApplicationException("绩效不存在");
        }
        rewardConfigDao.updateReward(request.getClientCode(),request.getBucket(),request.getTargetFinishValue(), request.getTarget());
        return new ApiResponse<>("success");
    }

    @AdminSignIgnore
    @RequestMapping("/reward/insert")
    public ApiResponse insertReward(@RequestBody RewardRequest request) {
        LOGGER.info("====,clientCode:{}",request.getClientCode());
        if (StringUtils.isEmpty(request.getClientCode())){
            return new ApiResponse(400,"请选择商户");
        }
        if (StringUtils.isEmpty(request.getBucket())){
            return new ApiResponse(400,"请选择阶段");
        }
        if (StringUtils.isEmpty(request.getTarget())){
            return new ApiResponse(400,"请输入目标绩效");
        }
        if (StringUtils.isEmpty(request.getFinishType())){
            return new ApiResponse(400,"请选择绩效模式");
        }
        List<RewardConfigEntity> rewardConfigEntities = rewardConfigDao.selectRewardByClientCodeAndBucket(request.getClientCode(), request.getBucket());
        LOGGER.info("== 绩效查询 ==,rewardConfigEntities:{}",rewardConfigEntities);
        if (CollectionUtils.isNotEmpty(rewardConfigEntities)){
            throw new ApplicationException("绩效已存在");
        }
        RewardConfigEntity rewardConfigEntity = new RewardConfigEntity();
        rewardConfigEntity.setConfigCode(request.getClientCode());
        rewardConfigEntity.setClientCode(request.getClientCode());
        rewardConfigEntity.setProductName(request.getClientCode());
        rewardConfigEntity.setBucket(request.getBucket());
        rewardConfigEntity.setTarget(Integer.parseInt(request.getTarget()));
        rewardConfigEntity.setFinishType(Integer.parseInt(request.getFinishType()));
        rewardConfigEntity.setFinishType(Integer.parseInt(request.getFinishType()));
        rewardConfigEntity.setXishu(10);
        rewardConfigEntity.setDeleted(false);
        if (request.getFinishType().equals("1")) {
            rewardConfigEntity.setExtraBonusForEach(0);
            rewardConfigEntity.setTargetFinishValue(0);
            rewardConfigEntity.setStageBonus(0);
            rewardConfigEntity.setMaxBonus(850);
            rewardConfigEntity.setSortNo(240);
            rewardConfigDao.insert(rewardConfigEntity);
            rewardConfigEntity.setTargetFinishValue(60);
            rewardConfigEntity.setStageBonus(100);
            rewardConfigEntity.setMaxBonus(850);
            rewardConfigEntity.setSortNo(241);
            rewardConfigDao.insert(rewardConfigEntity);
            rewardConfigEntity.setTargetFinishValue(80);
            rewardConfigEntity.setStageBonus(300);
            rewardConfigEntity.setMaxBonus(850);
            rewardConfigEntity.setSortNo(242);
            rewardConfigDao.insert(rewardConfigEntity);
            rewardConfigEntity.setTargetFinishValue(90);
            rewardConfigEntity.setStageBonus(500);
            rewardConfigEntity.setMaxBonus(850);
            rewardConfigEntity.setSortNo(243);
            rewardConfigDao.insert(rewardConfigEntity);
            rewardConfigEntity.setTargetFinishValue(100);
            rewardConfigEntity.setStageBonus(800);
            rewardConfigEntity.setMaxBonus(850);
            rewardConfigEntity.setSortNo(244);
            rewardConfigDao.insert(rewardConfigEntity);
        }else {
            rewardConfigEntity.setExtraBonusForEach(0);
            rewardConfigEntity.setTargetFinishValue(0);
            rewardConfigEntity.setStageBonus(0);
            rewardConfigEntity.setMaxBonus(850);
            rewardConfigEntity.setSortNo(240);
            rewardConfigDao.insert(rewardConfigEntity);
            rewardConfigEntity.setExtraBonusForEach(150);
            rewardConfigEntity.setTargetFinishValue(1);
            rewardConfigEntity.setStageBonus(0);
            rewardConfigEntity.setMaxBonus(850);
            rewardConfigEntity.setSortNo(241);
            rewardConfigDao.insert(rewardConfigEntity);
            rewardConfigEntity.setExtraBonusForEach(150);
            rewardConfigEntity.setTargetFinishValue(2);
            rewardConfigEntity.setStageBonus(0);
            rewardConfigEntity.setMaxBonus(850);
            rewardConfigEntity.setSortNo(242);
            rewardConfigDao.insert(rewardConfigEntity);
            rewardConfigEntity.setExtraBonusForEach(150);
            rewardConfigEntity.setTargetFinishValue(3);
            rewardConfigEntity.setStageBonus(0);
            rewardConfigEntity.setMaxBonus(850);
            rewardConfigEntity.setSortNo(243);
            rewardConfigDao.insert(rewardConfigEntity);
            rewardConfigEntity.setExtraBonusForEach(150);
            rewardConfigEntity.setTargetFinishValue(4);
            rewardConfigEntity.setStageBonus(0);
            rewardConfigEntity.setMaxBonus(850);
            rewardConfigEntity.setSortNo(244);
            rewardConfigDao.insert(rewardConfigEntity);
        }
        return new ApiResponse<>("success");
    }


}
